/*
 * CcLoginConf.java
 *
 * Created on __DATE__, __TIME__
 */

package clientPresentation;

import java.net.UnknownHostException;

import javax.swing.JOptionPane;

import client.ClientData;
import connection.CitizenCardInterface;
import connection.exception.CardErrorException;

/**
 *
 * @author  __USER__
 */
public class CcLoginConf extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;

	/** Creates new form CcLoginConf 
	 * @throws CardErrorException */
	public CcLoginConf() throws CardErrorException {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 * @throws CardErrorException 
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		authLabel = new javax.swing.JLabel();
		nextButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();
		nameLabel = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		authLabel.setText("Voc\u00ea est\u00e1 a autenticar-se como:");

		nextButton.setText("Next >");
		nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				nextButtonMouseClicked(evt);
			}
		});

		cancelButton.setText("Cancel");
		cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				cancelButtonMouseClicked(evt);
			}
		});

		nameLabel.setFont(new java.awt.Font("Segoe UI", 1, 12));
		nameLabel.setText("nameLabel");
		nameLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
		try {
			String[] subjectNameArray = CitizenCardInterface.getFirstLastName();
			String subjectName = subjectNameArray[0] + " "
					+ subjectNameArray[1];
			nameLabel.setText(subjectName);
		} catch (CardErrorException e) {
			JOptionPane.showMessageDialog(this,
					"O cartão não está conectado no leitor!!");
			LoginOptionMenu loginOption = new LoginOptionMenu();
			loginOption.setVisible(true);

			this.dispose();
			return;
		}

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(189, Short.MAX_VALUE)
								.addComponent(nextButton).addGap(18, 18, 18)
								.addComponent(cancelButton).addContainerGap())
				.addGroup(
						layout.createSequentialGroup()
								.addGap(60, 60, 60)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(
														nameLabel,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														215, Short.MAX_VALUE)
												.addComponent(
														authLabel,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														215,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(80, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addGap(30, 30, 30)
								.addComponent(authLabel)
								.addGap(18, 18, 18)
								.addComponent(nameLabel,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										39,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										34, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(cancelButton)
												.addComponent(nextButton))
								.addContainerGap()));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {
		LoginOptionMenu optionMenu = new LoginOptionMenu();
		this.setVisible(false);
		optionMenu.setVisible(true);

		this.dispose();
	}

	private void nextButtonMouseClicked(java.awt.event.MouseEvent evt) {
		try {
			boolean authResult = ClientData.getInstance().initWithCCAuth(
					nameLabel.getText());

			if (!authResult) {
				JOptionPane.showMessageDialog(this, "Falha na autentica��o");
				return;
			} else {
				MainMenu mf = new MainMenu();
				this.setVisible(false);
				mf.setVisible(true);
			}
		} catch (UnknownHostException ex) {
			JOptionPane.showMessageDialog(this,
					"Endere�o fornecido em configura��o do Servidor CERTCOP "
							+ "n�o � v�lido.");
			return;
		}
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					new CcLoginConf().setVisible(true);
				} catch (CardErrorException e) {
					e.printStackTrace();
				}
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JLabel authLabel;
	private javax.swing.JButton cancelButton;
	private javax.swing.JLabel nameLabel;
	private javax.swing.JButton nextButton;
	// End of variables declaration//GEN-END:variables

}